package cn.com.w.bootdemo.controller;

import cn.com.w.bootdemo.entity.User;
import cn.com.w.bootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;

    @GetMapping("/")
    public String getUser(){
        List<User> userList =  userService.getUser();
        return userList.toString();
    }


    @GetMapping("/dealUser")
    public void dealUserInfo(){
        List<User> userList =  userService.getUser();

        ComparatorUserInfo comparatorUserInfo = new ComparatorUserInfo();

        PriorityQueue<User> priorityQueue = new PriorityQueue<User>(comparatorUserInfo);
        priorityQueue.addAll(userList);
        while (true) {

            User user = priorityQueue.poll();
            System.out.println(user);
            if (user == null) {
                break;
            }
        }

    }

    class ComparatorUserInfo implements Comparator<User>{

        /*private List<User> userList;

        public ComparatorUserInfo(List<User> userList){
            this.userList = userList;
        }*/

        @Override
        public int compare(User o1, User o2) {
            if (o1.getLevel() >= o2.getLevel() && o1.getAge()>=o2.getAge()) {
                return -1;
            } else {
                return 1;
            }

        }

    }




}
